const db = uniCloud.database();
module.exports = {
	/**
	 * 时间查询
	 * @url template/db_api/pub/inc 前端调用的url参数地址
	 * data 请求参数 说明
	 * res 返回参数说明
	 * @params {Number} code 错误码，0表示成功
	 * @params {String} msg 详细信息
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, originalParam
		} = event;
		let {
			uniID,
			pubFun,
			vk,
			db,
			_
		} = util;
		let res = {
			code: 0,
			msg: 'ok'
		};
		// 业务逻辑开始----------------------------------------------------------- 

		let startTime = data.startTime
		let endTime = data.endTime || new Date().getTime()
		var dbName = data.dbName //集合表名
		let whereJson = data.whereJson || {}
		let match = {
			...whereJson,
			time: _.gte(startTime).lte(endTime)
		}
		var numField = data.numField || '$num'
		let res1 = await vk.baseDao.selects({
			dbName: dbName,
			pageIndex: data.pageIndex,
			pageSize: data.pageSize,
			whereJson: match,
			groupJson: {
				_id: _.$.dateToString({
					date: _.$.add([new Date(0), "$time"]),
					format: "%Y-%m-%d"
				}),
				totalPrice: _.$.sum(numField),
			},
			sortArr: [{
				"name": "_id",
				"type": "desc"
			}]
		});
		return {
			code: 0,
			msg: "操作成功",
			...res1
		}

		// 	// 业务逻辑结束-----------------------------------------------------------
		return res;
	}

}